home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / Libraries / C Internet Config / IC Component Debug ƒ / IC Debug.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-06  |  4.9 KB  |  233 lines  |  [TEXT/SPM ]

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #include <Folders.h>
  5. #include <Components.h>
  6.  
  7. #include "IC Types.h"
  8. #include "IC API.h"
  9. #include "IC Keys.h"
  10. #include "IC Component API.h"
  11. #include "IC Component Selectors.h"
  12.  
  13. #include "IC Component.h"
  14.  
  15. // Registration flags
  16. enum
  17. {
  18.     kRegisterLocally = 0,
  19.     kRegisterGlobally
  20. };
  21.  
  22. // globals used to register the component
  23. Handle gNameHdl;
  24. Handle gDescHdl;
  25. ComponentRoutineUPP gICComponentUPP;
  26. Component gICComponent;
  27.  
  28. // routine to register the component
  29. OSErr InstallComponent(void);
  30.  
  31. // sample routine to dump the names of the prefs resources
  32. void DumpPrefs(void);
  33. void DumpMaps(void);
  34.  
  35. // an ICInstance global
  36. ICInstance inst;
  37.  
  38. short gResFile;
  39.  
  40. // main test routine
  41. int main(void);
  42. int main(void){
  43.     ICError err;
  44.     ICDirSpecArray folder_spec;
  45.     Str255 email_address;
  46.     long attr;
  47.     long size;
  48.     long seed;
  49.     short vref;
  50.     long dirid;
  51.     OSErr oe;
  52.     
  53.     printf("Internet Config: C Example\n\n");
  54.     
  55.     printf("Registering the IC component...\n");
  56.     oe=InstallComponent();
  57.     printf("InstallComponent: %d\n",oe);
  58.     
  59.     // ICDisableComponent();
  60.     // disabled; we really only want to test the component
  61.     ICDisableLinkedCode();
  62.     
  63.     err = ICStart(&inst, 'CREA');            /* tell it your application creator */
  64.     printf("ICStart: %ld\n", err);
  65.     
  66.     if (err!=0L)
  67.         goto EndOfMain;
  68.     
  69.     Delay(60*2,&dirid);
  70.     
  71.     printf("ICUsingComponent: %s\n",ICUsingComponent(inst)?"true":"false");
  72.     
  73.     FindFolder(kOnSystemDisk,kPreferencesFolderType,kCreateFolder,&vref,&dirid);
  74.     
  75.     folder_spec[0].vRefNum = vref;                                /* search for prefs in root of the system */
  76.     folder_spec[0].dirID = dirid;                                    /* volume, obviously you'd use other things */
  77.     err = ICFindConfigFile(inst, 1, (ICDirSpecArrayPtr) &folder_spec);
  78.     printf("ICFindConfigFile: %ld\n", err);
  79.  
  80.     err = ICBegin(inst, icReadWritePerm);
  81.     printf("ICBegin: %ld %#lx\n", err,err);
  82.     size = sizeof(email_address);
  83.     err = ICGetPref(inst, "\pEmail", &attr, (Ptr) email_address, &size);
  84.     printf("ICGetPref: %ld\n", err);
  85.     
  86.     p2cstr(email_address);
  87.     printf("Your Email address is %s\n", email_address);
  88.     
  89.     Delay(60*2,&dirid);
  90.     
  91.     DumpPrefs();
  92.     
  93.     Delay(60*2,&dirid);
  94.     
  95.     DumpMaps();
  96.     
  97.     err = ICEnd(inst);
  98.     printf("ICEnd: %ld\n", err);
  99.  
  100.     err = ICGetSeed(inst, &seed);
  101.     printf("ICGetSeed: %ld = %ld\n", err, seed);
  102.     /* now monitor this seed to see if any preferences have changed */
  103.  
  104.     err = ICStop(inst);
  105.     printf("ICStop: %ld\n", err);
  106.     
  107.     fflush(stdout);
  108.     
  109. EndOfMain:
  110.  
  111.     CloseResFile(gResFile);
  112.     
  113.     return 0;
  114. }
  115.  
  116. void DumpPrefs(){
  117.     ICError err;
  118.     long count;
  119.     long i;
  120.     Str255 key;
  121.     
  122.     err = ICCountPref(inst, &count);
  123.     printf("ICCountPref: %ld, count is %ld\n", err,count);
  124.     for (i = 1; i <= count; i++) {
  125.         err = ICGetIndPref(inst, i, key);
  126.         p2cstr(key);
  127.         printf("  ICGetIndPref: %ld - %s\n", err, &key);
  128.     };
  129. }
  130.  
  131. void DumpMaps(){
  132.     ICError err;
  133.     long count;
  134.     Handle entries;
  135.     long i,pos;
  136.     ICMapEntry entry;
  137.     ICAttr attr;
  138.     
  139.     union {
  140.         OSType otype;
  141.         char ostr[6];
  142.     } mtype,mcreator,pcreator;
  143.     
  144.     // first need the mapping resource handle
  145.     err=ICGetPrefHandle(inst,kICMapping,&attr,&entries);
  146.     printf("ICGetPrefHandle: %ld\n",err);
  147.     if (err!=noErr)
  148.         return;
  149.     
  150.     
  151.     err=ICCountMapEntries(inst,entries,&count);
  152.     printf("ICCountMapEntries: %ld, count is %ld\n",err,count);
  153.     
  154.     if (err!=noErr)
  155.         return;
  156.     
  157.     if (count>0){
  158.         
  159.         for (i=1;i<=count;i++){
  160.             err=ICGetIndMapEntry(inst,entries,i,&pos,&entry);
  161.             
  162.             if (err!=noErr)
  163.                 printf("ICGetIndMapEntry: error %ld\n",err);
  164.             else {
  165.                 mtype.otype=entry.file_type;
  166.                 mcreator.otype=entry.file_creator;
  167.                 pcreator.otype=entry.post_creator;
  168.                 
  169.                 mtype.ostr[4]=mcreator.ostr[4]=pcreator.ostr[4]=0;
  170.                 
  171.                 printf("ICGetIndMapEntry: type %s, creator %s, post creator %s, ext %#s\n",
  172.                     mtype.ostr,mcreator.ostr,pcreator.ostr,entry.extension);
  173.             }
  174.         }
  175.     }
  176. }
  177.  
  178. /*
  179.     InstallComponent
  180.     
  181.     In order to test out the IC component, the code must be linked into the application,
  182.     and it has to be registered by hand.
  183.     
  184.     This routine will take care of the whole thing...
  185.     
  186. */
  187. OSErr InstallComponent(){
  188.     OSErr err;
  189.     Str255 cname="\pInternet Configuration";
  190.     Str255 cdesc="\pProvides shared configuration of internet preferences";
  191.     ComponentDescription desc;
  192.     extern pascal void IC_Hello(void);
  193.     FSSpec rs;
  194.     short vref;
  195.     long dirid;
  196.     
  197.     gNameHdl=NewHandle(256);
  198.     HLock(gNameHdl);
  199.     BlockMoveData(cname,*gNameHdl,cname[0]+1);
  200.     HUnlock(gNameHdl);
  201.     
  202.     gDescHdl=NewHandle(256);
  203.     HLock(gDescHdl);
  204.     BlockMoveData(cdesc,*gDescHdl,cdesc[0]+1);
  205.     HUnlock(gDescHdl);
  206.     
  207.     gICComponentUPP=NewComponentRoutineProc(InternetConfig);
  208.     
  209.     desc.componentType='PREF';
  210.     desc.componentSubType='ICAp';
  211.     desc.componentManufacturer='JPQE';
  212.     desc.componentFlags=0L;
  213.     desc.componentFlagsMask=0L;
  214.     
  215.     gICComponent=RegisterComponent(&desc,gICComponentUPP,kRegisterGlobally,gNameHdl,
  216.         gDescHdl,(Handle)0);
  217.     
  218.     if (gICComponent==(Component)0)
  219.         printf("Null component returned from RegisterComponent\n");
  220.     
  221.     FindFolder(kOnSystemDisk,kDesktopFolderType,kCreateFolder,&vref,&dirid);
  222.     FSMakeFSSpec(vref,dirid,"\pinternet config.rsrc",&rs);
  223.     
  224.     gResFile=FSpOpenResFile(&rs,fsRdPerm);
  225.     UseResFile(gResFile);
  226.     
  227.     return noErr;
  228. }
  229.  
  230.  
  231.  
  232.  
  233.